MFileName = sldd_to_m();
m_to_excel(MFileName);

function SlddFileName = sldd_to_m()
    [DDFile,~,~] = uigetfile('*.sldd','Select sldd file');
    if DDFile == 0
        return;
    end
    
    match_str = regexp(DDFile,'[a-zA-Z_0-9 ]+','match');
    SlddFileName = strcat(match_str{1},'.m');
    SlddNewFileName = strcat(match_str{1},'Sldd','.m');
    
    % open .sldd file
    MyDictObj = Simulink.data.dictionary.open(DDFile);
    % export data dictionary entries to mat or matlab file
    DataSectObj = getSection(MyDictObj,'Design Data');
    % export the entries from the Design Data section of sldd to MATLAB - 
    % file in current working folder
    exportToFile(DataSectObj,SlddFileName);
    
    SlddFile = fopen(SlddFileName,'r');
    SlddNewFile = fopen(SlddNewFileName,'w');
    
    while ~feof(SlddFile)
       tline = fgetl(SlddFile);
       match_str = contains(tline,"...");
       if match_str
          str = replace(tline,"...","");
          tline = fgetl(SlddFile);
          tline = replace(tline,"  "," ");
          tline = strcat(str,tline);
       end
       fprintf(SlddNewFile,tline); 
       fprintf(SlddNewFile,newline); 
    end
    fclose(SlddFile);
    fclose(SlddNewFile);
    delete(SlddFileName);
    movefile(SlddNewFileName,SlddFileName);
end

function m_to_excel(MFileName)
    DefaultFile = MFileName;
    template_file = 'template.xlsx';
    [XLSXFileName,~,~] = uiputfile('target.xlsx','Save as EXCEL');
    if XLSXFileName == 0
        return;
    end

    % Return a status of 1 when the operation is successful
    [status,~,~] = copyfile(template_file,XLSXFileName);
    if status == 1
        msgbox(['Please close ',XLSXFileName,' and try again!']);
        return;
    end

    % read m file.
    file_str = fileread(DefaultFile);

    % match signal
    exp_str = '\w+(?= = \w+.Parameter;)';
    var_str = regexp(file_str,exp_str,'match');
    sig_num = 0;

    for index = 1:length(var_str)

        % variable name
        var_name = var_str{index};

        % value
        exp_str = ['(?<=',var_name,'.Value = )[a-z.A-Z0-9]+'];
        match_str = regexp(file_str,exp_str,'match');
        if ~isempty(match_str)
            var_val = match_str{1};
        else
            var_val = [];
        end

        % data type
        exp_str = ['(?<=',var_name,'.DataType = '')\w+(?='';)'];
        match_str = regexp(file_str,exp_str,'match');
        if ~isempty(match_str)
            var_datatype = match_str{1};
        else
            var_datatype = [];
        end
    
        % minimum
        exp_str = ['(?<=',var_name,'.Min = )\w+(?=;)'];
        match_str = regexp(file_str,exp_str,'match');
        if ~isempty(match_str)
            var_min = match_str{1};
        else
            var_min = '[]';
        end

        % maximum
        exp_str = ['(?<=',var_name,'.Max = )\w+(?=;)'];
        match_str = regexp(file_str,exp_str,'match');
        if ~isempty(match_str)
            var_max = match_str{1};
        else
            var_max = '[]';
        end
    
        % headerfile
        exp_str = ['(?<=',var_name,'.CoderInfo.CustomAttributes.HeaderFile = '')\w+\.h'];
        match_str = regexp(file_str,exp_str,'match');
        if ~isempty(match_str)
            var_headerfile = match_str{1};
        else
            var_headerfile = 'Unknow';
        end
    
        sig_num = sig_num + 1;
        sig_cell{sig_num,1} = var_name;
        sig_cell{sig_num,2} = var_val;
        sig_cell{sig_num,3} = var_datatype;
        sig_cell{sig_num,4} = var_min;
        sig_cell{sig_num,5} = var_max;
        sig_cell{sig_num,6} = var_headerfile;
  
    end

    % write excel file 
    xlswrite(XLSXFileName,sig_cell,'Sheet1','B2');
    
end